<template>
  <DateTimePicker 
    :config='config' 
    :parentIndex='parentIndex' 
    :itemIndex='itemIndex' 
    :noWrong='noWrong' 
    @changeValue="getDateTime"
    ></DateTimePicker>
</template>
<script>
import DateTimePicker from "../form/date_time_picker";

export default {
  name: "time_picker",
  props: {
    dateTimePickerRec:Number,
    config: {
      type: Object,
      required: true
    },
    parentIndex: Number,
    itemIndex: Number
  },
  components: {
    DateTimePicker
  },
  watch: {
    dateTimePickerRec(n){
      this.getDateTime(this.parentIndex, this.itemIndex, this.config);
    },
  },
  data() {
    return {
      noWrong:true
    };
  },
  created() {
    this.getDateTime(this.parentIndex, this.itemIndex, this.config);
  },
  methods: {
    getDateTime(pi,ii,e){
      console.log('date_time_picker_2year=>',e.value)
      if(e.value){
        // 判断选择时间和当日时间的时间差，不超过2年（用当日时间向前推24个月，如果在选择时间之前可购买，在选择时间之后不可购买）
        let nowDate = this.getPreYearDate(2),
          chooseDate = e.value?e.value:'';
        var d1 = new Date(chooseDate.replace(/\-/g, "/"));
        var d2 = new Date(nowDate.replace(/\-/g, "/"));
        if (d1.getTime() >= d2.getTime()){
          this.noWrong = true;
          this.config.noWrong = true;
        }else{
          this.noWrong = false;
          this.config.noWrong = false;
        }
        this.$emit("changeValue", this.parentIndex, this.itemIndex, this.config);
      }
    },
    getPreYearDate(yearNum){
      var dd = new Date();
      var y = dd.getFullYear()-yearNum;
      var m = dd.getMonth() + 1 < 10 ? "0" + (dd.getMonth() + 1) : dd.getMonth() + 1; //获取当前月份的日期，不足10补0
      var d = dd.getDate() < 10 ? "0" + dd.getDate() : dd.getDate(); //获取当前几号，不足10补0
      return y + "-" + m + "-" + d;
    }
  }
};
</script>
<style lang="less">

</style>

